package com.czk.workqueue.consumer;

import com.czk.utils.RabbitmqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 工作队列消费者
 * 默认的情况是轮询的平均消费消息，消费者消费的数量是一致的
 *
 * @Author:ChenZhangKun
 * @Date: 2021/1/2 17:23
 */
public class FirstConsumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection czkConnection = RabbitmqUtils.getCzkConnection();
        Channel channel = czkConnection.createChannel();
        channel.queueDeclare("work", true, false, false, null);
        // 第二个参数表示是都开启自动确认
        // 此时如果发生宕机则会导致消息丢失而未被消费
        channel.basicConsume("work", true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者一接收到了 " + new String(body));
            }
        });
    }
}
